var $;
KISSY.use("node",function(S,Node){ 
    $=Node.all;  
	// alert($("body").height());
	// alert(KISSY.DOM.viewportHeight());

	$(".helper").on("click",function(){
		showDialog("帮助",$(this).attr("data-tip"),"form-horizontal");
		$(".helper-in").on("click",function(){
			closeDialog();
			showDialog("帮助",$(this).attr("data-tip"),"form-horizontal");
		});
	});
	
});

function __dialog(title,addon,top,width){
			var tstr = top?"top:"+top+"px;":"";
			var wstr = width?"width:"+width+"px;":"width:830px";
			var dialog =("<div class=\"bui-dialog\" style=\""+wstr+"visibility:visible;display:block;"+tstr+"\">");
			dialog+=("    <div class=\"bui-stdmod-header\">");
			dialog+=("        <div class=\"header-title\">");
			dialog+=("            "+title+"");
			dialog+=("        </div>");
			dialog+=("    </div>");
			dialog+=("    <div class=\"bui-stdmod-body\">");
			dialog+=("    "+addon);
			dialog+=("    </div>");
			dialog+=("    <div class=\"bui-stdmod-footer\">");
			dialog+=("        <button type=\"button\" class=\"button button-primary cancel\">");
			dialog+=("            关闭");
			dialog+=("        </button>");
			dialog+=("    </div>");
			dialog+=("    <a class=\"bui-ext-close\">");
			dialog+=("        <span class=\"bui-ext-close-x x-icon x-icon-normal\">");
			dialog+=("            ×");
			dialog+=("        </span>");
			dialog+=("    </a>");
			dialog+=("</div>");
			return dialog;
}
function showDialog(title,addon,show,top){
	top=top?top:KISSY.DOM.scrollTop()+KISSY.DOM.viewportHeight()/2-100;
	var width=($("."+show).innerWidth());
	var mask = ("<div style=\"width:100%;left:0;top:0;height:100%;position:fixed;\" class=\"bui-ext-mask\"></div>");
	$(mask).insertBefore("."+show);
	$(__dialog(title,addon,top,width)).insertBefore("."+show);
	$(".bui-ext-close-x").on("click",function(){$(".bui-ext-mask").remove();$(".bui-dialog").remove();$(".doc-content").removeClass("height240");});	
	$(".cancel").on("click",function(){$(".bui-ext-mask").remove();$(".bui-dialog").remove();$(".doc-content").removeClass("height240");});	
	// $(".bui-dialog").scrollIntoView();
}
function closeDialog(){
$(".bui-ext-close-x").fire("click");
}
//container 容器，count 总页数 pageindex 当前页数
function setPage(container, count, pageindex) {
var container = container;
var count = count;
var pageindex = pageindex;
var a = [];
  //总页数少于10 全部显示,大于10 显示前3 后3 中间3 其余....
  if (pageindex == 1) {
    a[a.length] = "<li class=\"disabled \"><a class=\"prev\" >&lt;&lt;</a></li>";
  } else {
    a[a.length] = "<a href=\"/?m=Home&c=Admin&a=msglist&p="+pageindex+"\" class=\"prev active\" style=\"border-left-width: 1px;background-color: #F5F5F5;\">&lt;&lt;</a>";
  }
  function setPageList() {
    if (pageindex == i) {
      a[a.length] = "<a href=\"/?m=Home&c=Admin&a=msglist&p="+i+"\" class=\"on active\" style=\"background-color: #F5F5F5;color:#999;\">" + i + "</a>";
    } else {
      a[a.length] = "<a href=\"/?m=Home&c=Admin&a=msglist&p="+i+"\" class=\"num\">" + i + "</a>";
    }
  }
  //总页数小于10
  if (count <= 10) {
    for (var i = 1; i <= count; i++) {
      setPageList();
    }
  }
  //总页数大于10页
  else {
    if (pageindex <= 4) {
      for (var i = 1; i <= 5; i++) {
        setPageList();
      }
      a[a.length] = "<a href=\"/?m=Home&c=Admin&a=msglist&p="+i+"\">" + count + "</a>";
    } else if (pageindex >= count - 3) {
      a[a.length] = "<a href=\"/?m=Home&c=Admin&a=msglist&p=1\">1</a>";
      for (var i = count - 4; i <= count; i++) {
        setPageList();
      }
    }
    else { //当前页在中间部分
      a[a.length] = "<a href=\"/?m=Home&c=Admin&a=msglist&p=1\">1</a>";
      for (var i = pageindex - 2; i <= pageindex + 2; i++) {
        setPageList();
      }
      a[a.length] = "<a href=\"/?m=Home&c=Admin&a=msglist&p="+count+"\">" + count + "</a>";
    }
  }
  if (pageindex == count) {
    a[a.length] = "<li class=\"active\"><a href=\"#\" class=\"next unclick end\">>></a></li>";
  } else {
    a[a.length] = "<a href=\"/?m=Home&c=Admin&a=msglist&p="+count+"\" class=\"next end\">>></a>";
  }
  
  //container.innerHTML = a.join("");
  container.empty();
  $(a.join("")).appendTo(container);
  //事件点击
  var pageClick = function() {
    var oAlink = container.children("a").getDOMNodes();
    var inx = pageindex; //初始的页码
	
    oAlink[0].onClick= function() { //点击上一页
      if (inx == 1) {
        return false;
      }
      inx--;
	  $('.pagination').attr("data-curpg",inx);
	  if($('.pagination').attr("data-action")=="msglist"){
		loadmsgpage(inx,$(".tab-nav-actived").attr("data-name"));
	  }else if($('.pagination').attr("data-action")=="admin"){
		loadshoppage(inx,$(".tab-nav-actived").attr("data-name"));
	  }
      setPage(container, count, inx);
      return false;
    }
    for (var i = 1; i < oAlink.length - 1; i++) { //点击页码
      oAlink[i].onClick = function() {
        inx = parseInt(this.innerHTML);
		$('.pagination').attr("data-curpg",inx);
		  if($('.pagination').attr("data-action")=="msglist"){
			loadmsgpage(inx,$(".tab-nav-actived").attr("data-name"));
		  }else if($('.pagination').attr("data-action")=="admin"){
			loadshoppage(inx,$(".tab-nav-actived").attr("data-name"));
		  }
        setPage(container, count, inx);
        return false;
      }
    }
    oAlink[oAlink.length - 1].onClick =  function() { //点击下一页
      if (inx == count) {
        return false;
      }
      inx++;
	  $('.pagination').attr("data-curpg",inx);
	  	  if($('.pagination').attr("data-action")=="msglist"){
			loadmsgpage(inx,$(".tab-nav-actived").attr("data-name"));
		  }else if($('.pagination').attr("data-action")=="admin"){
			loadshoppage(inx,$(".tab-nav-actived").attr("data-name"));
		  }
      setPage(container, count, inx);
      return false;
    }
  } ()
}